package com.example.Lezedata.ui.activity.baseClass;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.example.Lezedata.R;
import com.example.Lezedata.ui.activity.common.GBErrorView;
import com.example.Lezedata.util.CGHUDView;
import com.example.Lezedata.util.GBActivityManager;
import com.example.Lezedata.util.StatusBarUtil;

public class GBBaseVc extends AppCompatActivity {

    public TextView titleTextView;
    public FrameLayout baseContentView;
    private View contentView;
    public Toolbar toolbar;
    protected Context mContext;
    private CGHUDView cghudView;
    /// 是否需要添加到子父View
    public boolean needAddViewToSuperView = true;
    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(newBase);
        mContext = newBase;
        GBActivityManager.sharedManager().addActivity(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        if (needAddViewToSuperView){
            setStatusBar();
            if (getLayoutId() == 0) {
                throw new IllegalArgumentException("you must return a right resource id");
            }
            setView(getLayoutId());
            viewDidLoad();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        GBActivityManager.sharedManager().currentVc = this;
    }

    void setView(int layoutResID) {
        setContentView(R.layout.activity_base);
        if (layoutResID == 0){
            return;
        }
        // 已下为添加子view
        titleTextView = findViewById(R.id.titleTextView);
        baseContentView = findViewById(R.id.contentView);
        contentView = getLayoutInflater().inflate(layoutResID,baseContentView,true);
        configToolBar();
    }

    void configToolBar(){
        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // 默认无标题
        setTitle(title());

        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        toolbar.setBackgroundColor(navigationBackgroundColor());
        if (hideNavigationBack()){
            toolbar.setNavigationIcon(null);
        }else {
            toolbar.setNavigationIcon(navigationIcon());
            toolbar.setNavigationOnClickListener(navigationOnClickListener());
        }
    }

    protected void viewDidLoad(){}
    public View getContentView() {
        return contentView;
    }
    @Override
    public void setTitle(CharSequence title) {
        setTitleViewText(title);
    }
    @Override
    public void setTitle(int titleId) {
        setTitleViewText(getString(titleId));
    }

    void setTitleViewText(CharSequence title){
        if (titleTextView != null){
            titleTextView.setText(title);
        }
    }

    /**
     * @return activity的布局文件
     */
    protected int getLayoutId() {
        return 0;
    }
    /*是否隐藏返回按钮*/
    protected boolean hideNavigationBack() {
        return false;
    }

    private void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (isUseFullScreenMode()) {
                StatusBarUtil.transparencyBar(this);
            } else {
                StatusBarUtil.setStatusBarColor(this, getStatusBarColor());
            }

            if (isUseBlackFontWithStatusBar()) {
                StatusBarUtil.setLightStatusBar(this, true, isUseFullScreenMode());
            }
        }
    }

    /**
     * 是否设置成透明状态栏，即就是全屏模式
     */
    protected boolean isUseFullScreenMode() {
        return false;
    }

    /**
     * 更改状态栏颜色，只有非全屏模式下有效
     */
    protected int getStatusBarColor() {
        return R.color.white;
    }

    /**
     * 是否改变状态栏文字颜色为黑色，默认为黑色
     */
    protected boolean isUseBlackFontWithStatusBar() {
        return true;
    }

    public void jumpActivity(Class<?> clazz) {
        jumpActivityIntent(new Intent(this, clazz));
    }

    public void jumpActivityIntent(Intent intent) {
        startActivity(intent);
        overridePendingTransition(R.anim.activity_enter,R.anim.activity_stop);
    }

    public void jumpActivityIntent(Intent intent,int resultCode) {
        startActivityForResult(intent,resultCode);
        overridePendingTransition(R.anim.activity_enter,R.anim.activity_stop);
    }

    @Override
    public void onBackPressed() {
        finish();
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.activity_stop,R.anim.activity_exit);
    }

    public void finishActivityToClass(Class cls) {
        GBActivityManager.sharedManager().backToActivity(cls);
        overridePendingTransition(R.anim.activity_stop,R.anim.activity_exit);
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // 检测屏幕的方向：纵向或横向
        if (this.getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_LANDSCAPE) {
            //当前为横屏， 在此处添加额外的处理代码
        }
        else if (this.getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_PORTRAIT) {
            //当前为竖屏， 在此处添加额外的处理代码
        }
    }

    public int navigationIcon(){
        return R.drawable.app_back;
    }

    public View.OnClickListener navigationOnClickListener(){
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        };
    }

    public int navigationBackgroundColor(){
        return Color.WHITE;
    }

    public CharSequence title(){
        return null;
    }

    public void setOnMenuItemClickListener(Toolbar.OnMenuItemClickListener listener){
        toolbar.setOnMenuItemClickListener(listener);
    }

    public <T extends View> T findViewByTag(Object object) {
        return contentView.findViewWithTag(object);
    }

    private boolean HasHUD = false;
    public void hudShow(){
        HasHUD = true;
        cghudView = CGHUDView.showInView(mContext,baseContentView);
        baseContentView.setEnabled(false);
    }

    public void hudDismiss(){
        if (cghudView!=null) {
            HasHUD = false;
            baseContentView.setEnabled(true);
            cghudView.dismiss();
        }
    }

    public void addErrorView(){
        GBErrorView.showInView(mContext,baseContentView);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (HasHUD){
            return true;
        }
        return super.dispatchTouchEvent(ev);
    }
}